/*
 * NvmFileSource.h
 *
 *  Created on: 2019-01-02
 *      Author: chuanjiang.zh
 */

#ifndef NVMFILESOURCE_H_
#define NVMFILESOURCE_H_

#include <MediaSource.h>
#include <fstream>
#include "CMediaFileReader.h"
#include "ByteBuffer.h"


namespace av
{

class NvmFileSource : public MediaSource
{
public:
    static const char*	FILE_EXT;

public:
    NvmFileSource();
    virtual ~NvmFileSource();

    virtual int open(const std::string& url, const std::string& params);

    virtual void close();

    virtual bool isOpen();

    virtual bool getFormat(MediaFormat& fmt);

    virtual int getDuration();

    virtual int play();
    virtual int pause();
    virtual void stop();

    virtual int getState();

    virtual bool seekable();

    virtual int seek(int64_t offset);

    virtual int64_t getTime();

    virtual int setScale(float scale);

    virtual float getScale();

    virtual int read(AVPacket& pkt);

    virtual void interrupt();

    virtual bool isLive();

    virtual bool getMetaData(const std::string& key, std::string& value);

protected:
    av::MediaCodec getCodec(int codec);

    bool openParser(int codec);
    void closeParser();
    bool isParserOpen();

    bool parseRead(AVPacket& src, AVPacket& outPkt);
    bool readPacket(AVPacket& src, AVPacket& outPkt);

protected:
    CMediaFileReader    m_reader;
    MediaFormat	m_format;
    int64_t	m_curTime;

    AVCodecParserContext* m_parser;
    AVCodecContext* m_pContext;
    ByteBuffer  m_buffer;

    int64_t m_videoStartTs;
    int64_t m_audioStartTs;

};

}

#endif /* NVMFILESOURCE_H_ */
